
#ifndef _Map_H_
#define _Map_H_

#include "Utils/Vector2D.h"
#include "MapObstacle.h"
#include "Ship.h"

#include <string>
#include <vector>

struct PixelColour;
class RRT;

class Map {
  public:

    Map();
    ~Map();

    bool load(std::string filename);
    void render(void);
    void renderMinimap(void);

    // Set the "collision_space" argument to true if you want to use collision space map obstacles. 
    // This is used for path finding and building the traversal tree of the map for AI pathfinding. It uses 
    // collision space map obstacle definitions for determining ray segment collisions
    bool collideRaySegment(Vector2D start, Vector2D end, Vector2D &normal, 
                           Vector2D &collision_point, 
                           bool use_collision_space=false, bool tight_collision_space=false);


    // TODO: explain this function a bit more
    bool collideSphere(Vector2D centre, float radius, Vector2D &normal, Vector2D &collision_point,
                       Vector2D &constraint_free_point);

    bool pointInsideObstacle(Vector2D point, bool use_collision_space=false, bool tight_collision_space=false);
    bool pointInsideMap(Vector2D point);
    bool clipPointInsideMap(Vector2D &point, Vector2D &cnormal);

    float getWidth(void);
    float getHeight(void);

    Vector2D getMinCoord(void);
    Vector2D getMaxCoord(void);

  private:

    Vector2D min_coord, max_coord;
    std::vector<MapObstacle> all_obstacles;
    RRT *rrt;

    void renderBackground(void);
    void renderMapObstacles(void);
    void renderBackgroundMinimap(void);
    void renderMapObstaclesMinimap(void);

    void loadEntities(std::vector<PixelColour> map_image, int width, int height);
    bool isFlagColour(const PixelColour &col);

    void loadMapObstacles(std::vector<PixelColour> map_image, int width, int height);
    void fillObstacleRegion(std::vector<int> &obstacle_map, int width, int height, int label, int startx, int starty);
    bool isPixelObstacle(const PixelColour &pixel);

    Vector2D pixelToPosition(int pixel_x, int pixel_y, int width, int height);
};

#endif

